package com.example.dessertstoressm.controller;

import com.example.dessertstoressm.Service.CatalogService;
import com.example.dessertstoressm.domain.Account;
import com.example.dessertstoressm.domain.Category;
import com.example.dessertstoressm.domain.Product;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
//import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@SessionAttributes(value={"categoryId ","category" ,"productList"})
public class topController {




    @RequestMapping("/index")
    public String index(){
        return "index";
    }


    //top栏对应的url
    //主页
    @RequestMapping("/main")
    public String main(){
        return "catalog/main";
    }


    //购物车
    @RequestMapping("/cartForm")
    public String Cart(){
        return "cart/cart";
    }


    //类别
    @Autowired
    public CatalogService catalogService;
    //@Resource
    //public CatalogService catalogService;
    @GetMapping("categoryForm")
   public String category(Model model, @RequestParam("categoryId") String categoryId)
    {
         // HttpSession session = (HttpSession) req.getSession();
            //String categoryId = req.getParameter("categoryId");
            Category category = catalogService.getCategory(categoryId);
         System.out.println(category);
            System.out.print("yyyyyyyyyyy");//测试输出
            List<Product> productList = catalogService.getProductListByCategory(categoryId);
      //session.setAttribute("category" , category);
       // session.setAttribute("productList" , productList);
        model.addAttribute("category" , category);
        model.addAttribute("productList" , productList);
            return "catalog/category"  ;

    }

//    public String category()
//    {
//        return "catalog/category"  ;
//    }


    //我的订单
    @RequestMapping("listOrders")
    public String listOrders(){
        return "order/listOrders";
    }


    //我的信息


    //关于我们
    @RequestMapping("our.html")
    public String our(){
        return "our";
    }


    //登录
    @RequestMapping("signonForm")
    public String toSignonForm(){


        return "account/signon";
    }


    //注册
    @RequestMapping("registerForm")
    public String registerForm(){
        return "account/newAccount";
    }


    //登出
    @Autowired
    public Account account;
    @RequestMapping("signOut")
    public String signOut(HttpServletRequest req){

        HttpSession session = req.getSession();
        account = (Account) session.getAttribute("account");
        session.setAttribute("account",null);

        //账号设置为空之后返回主页
        return "catalog/main";
    }

    @PostMapping("searchProducts")
    public String searchAuto(HttpServletRequest req) throws IOException {
        String keyword =  new String(req.getParameter("keyword"));

        List<Product> productsBySearch = new ArrayList<Product>();
        productsBySearch = catalogService.searchProductList(keyword);
        HttpSession session = req.getSession();
        session.setAttribute("productsBySearch",productsBySearch);

        //日志
        /*
        Account account;
        account = (Account) session.getAttribute("account");

       if(account!=null) {

            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + " 搜索 “"+keyword+"” 关键词" ;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        else{
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + " 搜索 “"+keyword+"” 关键词" ;
            logService.insertLogInfo("未登录用户", logInfo);
        }

         */


        return "catalog/searchProduct";
    }


    @GetMapping("productAutoComplete")
    @ResponseBody
    public List<Product> productAutoComplete(HttpServletRequest req) throws UnsupportedEncodingException {
        String keyword = new String(req.getParameter("keyword"));
        List<Product> productsBySearch = new ArrayList<Product>();
        productsBySearch = catalogService.searchProductList(keyword);
        return productsBySearch;
    }
}
